package com.hbnu.ysc.dto;

import com.hbnu.ysc.pojo.BillRecord;
import com.hbnu.ysc.pojo.Insurance;
import com.hbnu.ysc.pojo.User;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class UserService {
    public static UserBasicService userBasicService = new UserBasicService();
    public static BillRecordService billRecordService = new BillRecordService();

    public static void serve(User user) {
        while (true){
            System.out.println("欢迎" + user.getName() + "登录！");

            Scanner sc = new Scanner(System.in);

            while (true){
                System.out.println("=====================================");
                System.out.println("1、退出登录  2、查询余额  3、存款  4、取款  5、转账  6、查看账单  7、保险服务");
                System.out.println("输入你要操作的编号：");
                int num = sc.nextInt();

                if (num==1){
                    return;
                }
                if (num==2){
                    System.out.println("=====================================");
                    System.out.println("余额为：" + user.getBalance());
                }
                if (num==3){
                    System.out.println("=====================================");
                    System.out.println("请输入要存款的金额：");
                    double amount = sc.nextDouble();
                    while (amount <= 0){
                        System.out.println("请输入正确的金额");
                        amount = sc.nextDouble();
                    }
                    userBasicService.saveBalance(user, user.getBalance()+amount);
                    BillRecord billRecord = new BillRecord("存款", user.name, amount, null, LocalDateTime.now());
                    billRecordService.saveBill(billRecord);
                }

                if (num==4){
                    System.out.println("=====================================");
                    System.out.println("请输入取款金额：");
                    double amount = sc.nextDouble();
                    while (amount <= 0){
                        System.out.println("请输入正确的金额");
                        amount = sc.nextDouble();
                    }
                    if (amount > user.getBalance()) {
                        System.out.println("余额不足！");
                    }
                    userBasicService.saveBalance(user, user.getBalance()-amount);
                    BillRecord billRecord = new BillRecord("取款", user.name, amount, null, LocalDateTime.now());
                    billRecordService.saveBill(billRecord);
                }

                if (num==5){
                    System.out.println("=====================================");


                    System.out.println("请输入转账账户：");
                    String account = sc.next();


                    User userVo = new User();
                    userVo.setName(account);
                    User targetUser = userBasicService.select(userVo);

                    if (targetUser == null) {
                        System.out.println("用户不存在！");
                        continue;
                    }
                    if (targetUser.getName().equals(user.getName())) {
                        System.out.println("不能转账给自己！");
                        continue;
                    }
                    System.out.println("请输入转账金额：");
                    double amount = sc.nextDouble();
                    while (amount <= 0){
                        System.out.println("请输入正确的金额");
                        amount = sc.nextDouble();
                    }
                    if (amount > user.getBalance()) {
                        System.out.println("余额不足！");
                        continue;
                    }

                    // 保存余额
                    userBasicService.saveBalance(user, user.getBalance()-amount);
                    userBasicService.saveBalance(targetUser, targetUser.getBalance()+amount);
                    BillRecord billRecord = new BillRecord("转账", user.name, amount, targetUser.name, LocalDateTime.now());
                    billRecordService.saveBill(billRecord);
                }

                if(num == 6){
                    System.out.println("=====================================");
                    List<BillRecord> billList = billRecordService.select(user.name);
                    billList.stream().forEach(System.out::println);

                }

                if (num==7){
                    System.out.println("=====================================");
                    System.out.println("1、保险查询  2、保险购买  3、查询购买的保险  4、查询自己的保险");
                    System.out.println("输入你要操作的编号：");
                    int num1 = sc.nextInt();
                    if (num1==1){
                        System.out.println("=====================================");
                        InsuranceService insuranceService = new InsuranceService();
                        ArrayList<Insurance> insurances = insuranceService.selectInsurance(new Insurance());
                        insurances.stream().forEach(System.out::println);
                    }

                    if (num1==2){
                        System.out.println("=====================================");
                        //TODO 保险购买
                    }
                    if (num1==3){
                        System.out.println("=====================================");
                        InsuranceService insuranceService = new InsuranceService();
                        ArrayList<Insurance> insurances = insuranceService.selectInsBill(
                                Insurance
                                .builder()
                                .buyName(user.name)
                                .build());

                        insurances.stream().forEach(System.out::println);
                    }
                    if (num1==4){
                        System.out.println("=====================================");
                        InsuranceService insuranceService = new InsuranceService();
                        ArrayList<Insurance> insurances = insuranceService.selectInsBill(
                                Insurance
                                        .builder()
                                        .targetName(user.name)
                                        .build());
                    }
                }

            }
        }

    }
}
